import baseRequest from '../../request'


import {translateJSDOM} from '../../tool'
// import qs from 'qs'

interface myTimeTable {
    titleList:any[]
    contentList: any[]
}

export const getTimeTable = (session: string,selectData?: {}) => {
    return new Promise(async (resolve) => {

        let res = await baseRequest.get('http://jwmis.wuyiu.edu.cn/jsxsd/xskb/xskb_list.do',session)

        // console.log(res.data);
        const $:any = translateJSDOM(res.data)

        let myTimeTable:myTimeTable = {
            titleList: [],
            contentList: []
        }

        // 获取到表格dom节点，并遍历每一个tr(每一行进行) => 获取内容
        $("#kbtable > tbody > tr").each((index:any, item:number) => {
            // 获取表头 => 也就是第一行单元格
            if (index === 0) {
                // 获取第一行里的每个的单元格内容，并生成表头数组
                $(item).children().each((i:any, t:number) => {
                    if (i === 0) {
                        // 第一个为空内容，此处自定义设置表头
                        myTimeTable.titleList.push('节次');
                    } else {
                        myTimeTable.titleList.push($(t).text());
                    }
                });
            }
            // 获取非表头（非第一行）内容 => 生成表格内容数组 => 对应表头的一个对象数组
            if (index !== 0) {
                //获取表格内容
                let nowObj = $(item).children();
                //创建每一行内容对象
                myTimeTable.contentList[index - 1] = {};
                //给该对象加入每个表头对应的内容
                Object.keys(nowObj).forEach((t, i) => {
                    if (i < myTimeTable.titleList.length) {
                        if (i !== undefined) {
                            if(i!==0){
                                let text =  $(nowObj[i]).text().replace(/\s+/g,"");
                                myTimeTable.contentList[index - 1][myTimeTable.titleList[i]] = text.split("----------------------");
                            }else {
                                //为节次时不需要切割成为数组
                                myTimeTable.contentList[index - 1][myTimeTable.titleList[i]] = $(nowObj[i]).text().replace(/\s+/g,"");
                            }
                        }
                    }
                });
            }
        });
        // console.log(myTimeTable);
        resolve(myTimeTable)
    })

}

